﻿using MysqlforDataWatch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tools.ListOperation.StatisticAccListOperation
{
    public static class StatisticCombined
    {
        public static List<StatisticAllHalfHour> statisticcombineV2(List<StatisticAllHalfHour> orlist, string vehicleid)
        {
            if (orlist != null)
            {
                var ListperHalfHour = orlist.GroupBy(x => new
                {
                    x.chantitle,
                    x.datadate.Value.Year,
                    x.datadate.Value.Month,
                    x.datadate.Value.Day,
                    x.datadate.Value.Hour,
                    x.datadate.Value.Minute
                }
                  ).Select(x => new
                  {
                      Id = x.Min(a => a.id),
                      VehicleID = vehicleid,
                      Filename = x.Min(a => a.filename),
                      Datadate = x.Min(a => a.datadate),
                      x.Key.chantitle,

                      Max = x.Max(a => a.max),
                      Min = x.Min(a => a.min),
                      Range = x.Max(a => a.max) - x.Min(a => a.min),
                      Rms = x.Sum(a => a.rms) / Math.Sqrt(x.Count()),

                      //Damage = Math.Round((double)x.Sum(a => a.Damage), 0),

                  }).OrderBy(b => b.chantitle).ThenBy(b => b.Datadate).ToList();

                List<StatisticAllHalfHour> ListperHalfHourConvert = ListperHalfHour.Select(a => new StatisticAllHalfHour
                {
                    id = a.Id,
                    vehicle = a.VehicleID,
                    filename = a.Filename,
                    datadate = a.Datadate,
                    chantitle = a.chantitle,
                    max = a.Max,
                    min = a.Min,
                    range = a.Range,
                    rms = a.Rms,
                    //Damage=a.Damage

                }).ToList();


                return ListperHalfHourConvert;
            }
            else
            {
                return null;
            }

        }
    }
}
